from PIL import Image
import numpy as np
import os


image_list = os.listdir('./img')
image_list.sort(key=lambda x:int(os.path.splitext(x)[0]))
utf8_indices = {int(os.path.splitext(image)[0]):i for i,image in enumerate(image_list)}
image_list = ['img/'+file for file in image_list]

black = '■'
white = '□'

font_left = []
font_right = []
for i,image in enumerate(image_list):
    print(i,image)
    image = Image.open(image).convert('1')
    image = image.resize((16,14))
    img = np.array(image)
    bin_image = ['0x00','0x00']
    for row in img:
        line = ''
        for col in row:
            if col:
                line += '0'
            else:
                line += '1'


        bin_image[0] += ('0x{:0>2}'.format(hex(int(line[:8],2))[2:]))
        bin_image[1] += ('0x{:0>2}'.format(hex(int(line[8:],2))[2:]))
    bin_image[0] += '0x00'
    bin_image[1] += '0x00'
    bin_image[0] = bin_image[0].replace('0x','\\x')
    bin_image[1] = bin_image[1].replace('0x','\\x')

    font_left.append(bin_image[0])
    font_right.append(bin_image[1])

unicode_left = 'UNICODE_LEFT=[\n'
unicode_right = 'UNICODE_RIGHT=[\n'
unicode_indices = 'UNICODE_INDICES={\n'
for image in font_left:
    unicode_left += '    b\'{}\',\n'.format(image)
for image in font_right:
    unicode_right += '    b\'{}\',\n'.format(image)

for key in utf8_indices:
    unicode_indices += '    {}:{},\n'.format(key,utf8_indices[key])

unicode_left += ']'
unicode_right += ']'
unicode_indices += '}'
with open('./utf8font.py','w') as f:
    f.write(unicode_left+'\n')
    f.write(unicode_right+'\n')
    f.write(unicode_indices+'\n')






